package com.qf.kdgroup.gdbackend.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.kdgroup.gdbackend.dto.ResultBean;
import com.qf.kdgroup.gdbackend.entity.Auth;
import com.qf.kdgroup.gdbackend.entity.Role;
import com.qf.kdgroup.gdbackend.entity.RoleAuth;
import com.qf.kdgroup.gdbackend.entity.User;
import com.qf.kdgroup.gdbackend.mapper.RoleAuthMapper;
import com.qf.kdgroup.gdbackend.mapper.RoleMapper;
import com.qf.kdgroup.gdbackend.mapper.UserMapper;
import com.qf.kdgroup.gdbackend.service.RoleService;
import com.qf.kdgroup.gdbackend.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageInfo selectAllByPage(PageVO pageVO) {
        PageHelper.startPage(pageVO.getPageNum().intValue(), pageVO.getPageSize().intValue());
        List<Role> roles = roleMapper.selectRolesList(pageVO.getKeyword());
        PageInfo pageInfo = new PageInfo(roles);
        return pageInfo;
    }

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleAuthMapper roleAuthMapper;

    @Override
    public ResultBean deleteByRoleId(Long roleid) {

        User user = userMapper.selectByRoleId(roleid);

        if (user != null) {
            return ResultBean.error("删除失败，有用户关联该角色");
        }

        int res = roleMapper.deleteByPrimaryKey(roleid);

        roleAuthMapper.deleteByRoleId(roleid);
        return res > 0 ? ResultBean.success("删除成功") : ResultBean.error("删除失败");
    }

    @Override
    public ResultBean add(Role role) {

        int res = roleMapper.insertSelective(role);

        List<RoleAuth> roleAuths = new ArrayList<RoleAuth>();
        for (Long authid : role.getAuthids()) {
            roleAuths.add(new RoleAuth(null,role.getRoleid(),authid));
        }
        if(role.getAuthids().size()>0){
            roleAuthMapper.addByList(roleAuths);
        }
        return res > 0 ? ResultBean.success() : ResultBean.error();
    }

    @Override
    public Role findByRoleId(Long roleid) {

        Role role = roleMapper.selectByPrimaryKey(roleid);
        List<RoleAuth> roleAuths = roleAuthMapper.selectListByRoleId(roleid);
        List<Long> authids = new ArrayList<>();
        for (RoleAuth roleAuth : roleAuths) {
            authids.add(roleAuth.getAuthid());
        }
        role.setAuthids(authids);
        return role;
    }

    @Override
    public ResultBean update(Role role) {

        roleAuthMapper.deleteByRoleId(role.getRoleid());
        int res = roleMapper.updateByPrimaryKeySelective(role);
        List<RoleAuth> roleAuths = new ArrayList<RoleAuth>();
        for (Long authid : role.getAuthids()) {
            roleAuths.add(new RoleAuth(null,role.getRoleid(),authid));
        }
        roleAuthMapper.addByList(roleAuths);
        return  ResultBean.success();
    }
    
}
